package com.jameschen.bmical;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;


public class BMICalActivity extends Activity {
    private Button button_cal;
    private Button button_ab;
    private EditText field_weight;
    private EditText field_height;
    private TextView text_result;
    private TextView text_advice;
    private Spinner spinner;
    private static final int MENU_ABOUT = Menu.FIRST;
    private static final int MENU_QUIT = Menu.FIRST+1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        setListeners();
        setAdapter();
    }

    private void setAdapter() {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(BMICalActivity.this, R.array.gender_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }
    private void setListeners() {
        button_cal.setOnClickListener(BMICalculate);
        button_ab.setOnClickListener(aboutDialog);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }

    private void findViews() {
    	spinner = (Spinner)findViewById(R.id.gender_selecter);
        button_cal = (Button)findViewById(R.id.button_cal);
        button_ab = (Button)findViewById(R.id.button_about);
        field_weight = (EditText)findViewById(R.id.input_weight);
        field_height = (EditText)findViewById(R.id.input_height);
        text_result = (TextView)findViewById(R.id.text_result);
        text_advice = (TextView)findViewById(R.id.text_advice);
    }

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	        Toast.makeText(BMICalActivity.this, "Choose gender", Toast.LENGTH_LONG).show();
	    }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // Do nothing
        }
    };

    private OnClickListener aboutDialog  = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            openAboutDialog();
        }
    };

    private OnClickListener BMICalculate = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            DecimalFormat df = new DecimalFormat("0.00");
            try {
                float weight = Float.parseFloat(field_weight.getText().toString());
                float height = Float.parseFloat(field_height.getText().toString())/100;
                float bmi_result = weight/(height*height);

                text_result.setText(df.format(bmi_result));

                // Display advice
                if (bmi_result < 18.5) {
                    text_advice.setText(R.string.text_advice_1);
                } else if (bmi_result < 22) {
                    text_advice.setText(R.string.text_advice_2);
                } else if (bmi_result < 24) {
                    text_advice.setText(R.string.text_advice_3);
                } else if (bmi_result <30) {
                    text_advice.setText(R.string.text_advice_4);
                } else if (bmi_result <40) {
                    text_advice.setText(R.string.text_advice_5);
                } else {
                    text_advice.setText(R.string.text_advice_6);
                }
            } catch(Exception e) {
                Toast.makeText(BMICalActivity.this, R.string.error_message, Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_ABOUT, 0, R.string.menu_about);
        menu.add(0, MENU_QUIT, 0, R.string.menu_quit);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
        case MENU_ABOUT:
            openAboutDialog();
            break;
        case MENU_QUIT:
            finish();
            break;
        }
        return false;
    }

    private void openAboutDialog() {
        new AlertDialog.Builder(BMICalActivity.this)
                        .setTitle(R.string.text_about_title)
                        .setMessage(R.string.text_about_message)
                        .setPositiveButton(R.string.lable_ok,
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                            }
                                        })
                        .setNegativeButton(R.string.lable_home,
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Uri uri = Uri.parse(getString(R.string.url_home));
                                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                                startActivity(intent);
                                            }
                                        })
                        .show();
    }
}